using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Xml;
using Graffiti.Core;
using System.Web;

namespace Graffiti.Slide.Show
{
    /// <summary>
    /// Base Class for the various Slide.Show Graffiti Plugins
    /// </summary>
    public abstract class BaseSlideShowPlugin : GraffitiEvent
    {

        #region fields

        protected readonly string _resDownloadSilverlight = "<a href=\"http://go.microsoft.com/fwlink/?LinkID=124807\" style=\"text-decoration: none;\"><img src=\"http://go.microsoft.com/fwlink/?LinkId=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-style: none\" /></a>\n";

        #endregion

        #region Properties

        private string _width = "100%";
        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private string _height = "400";
        public string Height
        {
            get { return _height; }
            set { _height = value; }
        }

        private string _bgColor = "#FFF";
        public string BackgroundColor
        {
            get { return _bgColor; }
            set { _bgColor = value; }
        }

        private string _theme = "SimpleTheme";
        public string Theme
        {
            get { return _theme; }
            set { _theme = value; }
        }

        private string _postName = string.Empty;
        public string PostName
        {
            get { return _postName; }
            set { _postName = value; }
        }

        private string _configXML = string.Empty;
        public string ConfigXML
        {
            get { return _configXML; }
            set { _configXML = value; }
        }

        #endregion

        #region Constructor
        public BaseSlideShowPlugin()
        {
        }
        #endregion

        #region Init
        public override void Init(GraffitiApplication ga)
        {
            ga.BeginRequest += new EventHandler(ga_BeginRequest);
            ga.RenderPostBody += new RenderPostBodyEventHandler(ga_RenderPostBody);
        }

        #endregion

        #region GraffitiEvent Properties

        public override bool IsEditable
        {
            get { return true; }
        }

        #endregion

        #region Events

        protected virtual void ga_BeginRequest(object sender, EventArgs e)
        {
        }

        protected virtual void ga_RenderPostBody(StringBuilder sb, PostEventArgs e)
        {
        }

        #endregion

        #region Configuration

        protected virtual void AddBaseFormElements(FormElementCollection fec)
        {
            fec.Add(new TextFormElement("postName", "Post Name to display photo gallery on", "The post name of the post that the gallery should be displayed on. It will be rendered below the post body."));
            fec.Add(new TextFormElement("width", "Photo Gallery Width", "The width  of the photo gallery control in pixels or a percentage (i.e. 400 or 100%)."));
            fec.Add(new TextFormElement("height", "Photo Gallery Height", "The height of the photo gallery control in pixels or a percentage (i.e. 400 or 100%)."));
            fec.Add(new TextFormElement("bgColor", "Background Color", "Enter the HTML background color the gallery should use"));

            ListFormElement lfe = new ListFormElement("theme", "Gallery Theme", "Select an existing theme or choose Custom XML to create your own below.");
            foreach (ListItemFormElement item in GetThemes())
            {
                lfe.Add(item);
            }
            fec.Add(lfe);

            fec.Add(new TextAreaFormElement("configXML", "Custom Theme XML Configuration", "Leave this blank unless you selected Custom XML as your theme. See <a href=\"http://www.codeplex.com/SlideShow2\">online docs</a> for creating a custom theme.", 4));
        }

        protected virtual void AddAboutFormElements(FormElementCollection fec)
        {
            fec.Add(new MessageElement("about", "<br /><strong>About this plugin</strong>",
                "This plugin uses the open source <a href=\"http://www.codeplex.com/SlideShow2\">Slide.Show2</a> gallery control, which uses <a href=\"http://www.microsoft.com/silverlight/\">Microsoft Silverlight 2.0</a> to render. Graffiti plugin written by <a href=\"http://kevinharder.com\">Kevin Harder</a>."));
        }

        protected virtual void AddBaseConfigValues(NameValueCollection nvc)
        {
            nvc["postName"] = PostName;
            nvc["width"] = Width.ToString();
            nvc["height"] = Height.ToString();
            nvc["bgColor"] = BackgroundColor;
            nvc["theme"] = Theme;
            nvc["configXML"] = ConfigXML;
        }

        protected virtual StatusType SetBaseValues(System.Web.HttpContext context, NameValueCollection nvc)
        {
            Data data = new Data();
            PostName = nvc["postName"];
            Post post = data.GetPost(PostName);
            if (post == null)
            {
                PostName = Util.CleanForUrl(PostName);
                post = data.GetPost(PostName);

                if (post == null)
                {
                    SetMessage(context, "Could not find a post with that post name.");
                    return StatusType.Error;
                }
            }

            Width = nvc["width"];
            Height = nvc["height"];
            BackgroundColor = nvc["bgColor"];
            Theme = nvc["theme"];
            ConfigXML = nvc["configXML"];

            return StatusType.Success;
        }

        protected virtual List<ListItemFormElement> GetThemes()
        {
            List<ListItemFormElement> themes = new List<ListItemFormElement>();
            themes.Add(new ListItemFormElement("Simple", "SimpleTheme"));
            themes.Add(new ListItemFormElement("Light", "LightTheme"));
            themes.Add(new ListItemFormElement("Dark", "DarkTheme", true));
            themes.Add(new ListItemFormElement("Custom XML", "XML"));
            return themes;
        }

        #endregion

        #region protected void HandleConfigRequest(HttpContext context)
        protected void HandleConfigRequest(HttpContext context)
        {
            Macros macros = new Macros();

            context.Response.Clear();
            context.Response.Cache.SetCacheability(HttpCacheability.Public);

            StringWriter sw = new StringWriter();
            sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            XmlTextWriter writer = new XmlTextWriter(sw);
            writer.WriteRaw(this.ConfigXML);

            // save XML into response
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/xml";
            context.Response.Write(sw.ToString());
            context.Response.End();
        }

        #endregion

        protected bool ConvertStringToBool(string checkValue)
        {
            if (string.IsNullOrEmpty(checkValue))
                return false;
            else if (checkValue == "checked" || checkValue == "on")
                return true;
            else
                return bool.Parse(checkValue);
        }

    }
}
